/**
 * Created by Administrator on 2015/10/26 0026.
 */
var nodeFn = require('when/node');
var mongodb = require('mongodb');
var ObjectId = mongodb.ObjectId
var MongoClient = mongodb.MongoClient;
var Collection = mongodb.Collection;
var Cursor = mongodb.Cursor;


var mongoUrl = "mongodb://localhost:27017/video";

exports.connect = nodeFn.lift(MongoClient.connect);
exports.insert = nodeFn.lift(Collection.prototype.insert);
exports.insertOne = nodeFn.lift(Collection.prototype.inserOne);
exports.inserMany = nodeFn.lift(Collection.prototype.insertMany);


exports.findOne = nodeFn.lift(Collection.prototype.findOne);

dbPromise = null;

exports.getDb = function () {
    if (dbPromise) {
        return dbPromise
    } else {
        console.log(121);
        return exports.connect(mongoUrl);
    }
}

exports.dispose = function () {
    exports.getDb.done(function (db) {
        db.close();
    })
}

exports.ObjectId = ObjectId

